package com.ruoyi.laiwu.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.core.domain.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.ruoyi.laiwu.domain.vo.LwAlarmDetailVo;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import com.ruoyi.laiwu.domain.bo.LwAlarmExtendBo;
import com.ruoyi.laiwu.domain.vo.LwAlarmExtendVo;
import com.ruoyi.laiwu.domain.LwAlarmExtend;
import com.ruoyi.laiwu.mapper.LwAlarmExtendMapper;
import com.ruoyi.laiwu.service.ILwAlarmExtendService;

import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 告警扩展Service业务层处理
 *
 * @author ruoyi
 * @date 2024-03-08
 */
@RequiredArgsConstructor
@Service
public class LwAlarmExtendServiceImpl implements ILwAlarmExtendService {

    private final LwAlarmExtendMapper baseMapper;

    /**
     * 查询告警扩展
     */
    @Override
    public LwAlarmExtendVo queryById(Long id){
        return baseMapper.selectVoById(id);
    }

    /**
     * 查询告警扩展列表
     */
    @Override
    public TableDataInfo<LwAlarmExtendVo> queryPageList(LwAlarmExtendBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<LwAlarmExtend> lqw = buildQueryWrapper(bo);
        Page<LwAlarmExtendVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询告警扩展列表
     */
    @Override
    public List<LwAlarmExtendVo> queryList(LwAlarmExtendBo bo) {
        LambdaQueryWrapper<LwAlarmExtend> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<LwAlarmExtend> buildQueryWrapper(LwAlarmExtendBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<LwAlarmExtend> lqw = Wrappers.lambdaQuery();
        lqw.eq(bo.getSubId() != null, LwAlarmExtend::getSubId, bo.getSubId());
        lqw.eq(StringUtils.isNotBlank(bo.getDevice_code()), LwAlarmExtend::getDevice_code, bo.getDevice_code());
        return lqw;
    }

    /**
     * 新增告警扩展
     */
    @Override
    public Boolean insertByBo(LwAlarmExtendBo bo) {
        LwAlarmExtend add = BeanUtil.toBean(bo, LwAlarmExtend.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改告警扩展
     */
    @Override
    public Boolean updateByBo(LwAlarmExtendBo bo) {
        LwAlarmExtend update = BeanUtil.toBean(bo, LwAlarmExtend.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(LwAlarmExtend entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除告警扩展
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }

    @Override
    public LwAlarmExtendVo queryByAlarmId(Long subId) {
        LambdaQueryWrapper<LwAlarmExtend> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(LwAlarmExtend::getSubId,subId);
        List<LwAlarmExtendVo> lwAlarmDetailVos = baseMapper.selectVoList(wrapper);
        if(lwAlarmDetailVos.isEmpty()){
            return null;
        }
        return lwAlarmDetailVos.get(0);
    }
}
